/*
 * Created: 2024/9/4
 * Author:  hineven
 * See LICENSE for licensing.
 */
#include "core/blobres.h"

MI_NAMESPACE_BEGIN

void BlobResourceHandle::CheckAccess(BlobResourceAccessFlags required_access) const {
    if ((access_ & required_access) != required_access) {
        throw std::runtime_error("Access violation");
    }
}

const void * BlobResourceHandle::ReadBlobZeroCopy() {
    CheckAccess(BlobResourceAccessFlagBits::kRead);
    return blob_resource_->ReadBlobZeroCopy();
}

void BlobResourceHandle::ReadBlob(size_t size, void* data) {
    CheckAccess(BlobResourceAccessFlagBits::kRead);
    blob_resource_->ReadBlob(size, data);
}

size_t BlobResourceHandle::GetSize() {
    CheckAccess(BlobResourceAccessFlagBits::kRead);
    return blob_resource_->GetSize();
}

void* BlobResourceHandle::WriteBlob(size_t size, const void* data) {
    CheckAccess(BlobResourceAccessFlagBits::kWrite);
    return blob_resource_->WriteBlob(size, data);
}

size_t BlobResourceHandle::GetPos() const {
    CheckAccess(BlobResourceAccessFlagBits::kRead);
    return blob_resource_->GetPos();
}

size_t BlobResourceHandle::SetPos() {
    CheckAccess(BlobResourceAccessFlagBits::kWrite);
    return blob_resource_->SetPos();
}

MI_NAMESPACE_END